//
//  DFCoreDataHelper.m
//  换换圈
//
//  Created by 艾柯斯 on 14-11-12.
//  Copyright (c) 2014年 艾柯斯. All rights reserved.
//

#import "DFCoreDataHelper.h"


@interface DFCoreDataHelper() {
}

@end


@implementation DFCoreDataHelper



#pragma mark 数据库操作


+ (id)synchEntity:(NSString*)entityName key:(NSString*)key value:(id)value context:(NSManagedObjectContext*)context {

    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:entityName];
    [request setPredicate:[NSPredicate predicateWithFormat:@"%@ == %@",key,value]];
    id  object =  [[context executeFetchRequest:request error:nil] firstObject];
    if(object) {
        return object;
    }else {
        object = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
        [object setValue:value forKey:key];
        return object;
    }
}
  

/*

- (void)deleteObject:(NSManagedObject*)obj {
    [_context deleteObject:obj];
}

- (void)deleteObjects:(NSArray*)entities {
    for (NSManagedObject* entity in entities) {
        [_context deleteObject:entity];
    }
}

- (void)deleteAllForEntityForName:(NSString*)entityName {
    NSArray* entities = [self arrayWithEntityName:entityName];
    [self deleteObjects:entities];
}

- (void)deleteAllForEntityForName:(NSString*)entityName where:(NSString*)where {
    NSArray* entities = [self arrayWithEntityName:entityName where:where];
    [self deleteObjects:entities];
}


-(NSArray*)arrayWithEntityName:(NSString*)entityName {
    return [self arrayWithEntityName:entityName where:nil sort:nil];
}

- (NSArray *)arrayWithEntityName:(NSString *)entityName where:(NSString *)where {
    return [self arrayWithEntityName:entityName where:where sort:nil];
}


-  (NSArray*)arrayWithEntityName:(NSString*)entityName where:(NSString*)where sort:(NSSortDescriptor*)sort {
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:entityName];
    if(where.length)
        [request setPredicate:[NSPredicate predicateWithFormat:where]];
    if(sort)
        [request setSortDescriptors:[NSArray arrayWithObject:sort]];
    NSError* error = nil;
    NSArray *entities = [_context executeFetchRequest:request error:&error];
    if (error) {
        NSLog(@"操作失败:%@",error.localizedDescription);
    }
    return entities;
}


- (id)entityWithEntityName:(NSString *)entityName where:(NSString *)where {
    NSArray* entities = [self arrayWithEntityName:entityName where:where];
    if(entities.count) {
        return [entities firstObject];
    }
    return nil;
}




- (void)save {
    NSError* error = nil;
    if (![_context save:&error]) {
        NSLog(@"操作失败:%@",error.localizedDescription);
    }
}


- (void)openDatabase:(NSString*)database {
    NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
    NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
    NSURL *url = [NSURL fileURLWithPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:database]];
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES],NSInferMappingModelAutomaticallyOption, nil];
    NSError *error = nil;
    if(![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:&error]) {
        NSLog(@"打开数据库错误:%@:尝试初始化数据库",[error localizedDescription]);
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        [fileMgr removeItemAtURL:url error:nil];
        [self openDatabase:database];
        return;
    }
    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
    context.persistentStoreCoordinator = psc;
    _context = context;
}
*/

@end
